﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using InfoSvc.Data.WPP;
using System.Text.RegularExpressions;

namespace WPP_Console
{
    /// <summary>
    /// Interaction logic for CustomerIdentification.xaml
    /// </summary>
    public partial class CustomerIdentification : Window
    {
        OrderWindow orderWindow;

        public CustomerIdentification()
        {
            InitializeComponent();
        }

        public CustomerIdentification(OrderWindow ow)
        {
            InitializeComponent();

            orderWindow = ow;
        }

        private void btnSave_Click(object sender, RoutedEventArgs e)
        {
            if (txtID.Text == "" || txtID.Text == null)
            {
                MessageBox.Show("Please enter a value for ID.");
                return;
            }

            string id = "";

            if (rbDrivers.IsChecked.Value)
            {
                if (cmbState.SelectedItem == null)
                {
                    MessageBox.Show("Please make a selection for State.");
                    return;
                }

                string state = cmbState.SelectedItem.ToString().Substring(38);
                switch (state)
                {
                    case "Alabama": id += "AL_"; break;
                    case "Alaska": id += "AK_"; break;
                    case "Arizona": id += "AZ_"; break;
                    case "Arkansas": id += "AR_"; break;
                    case "California": id += "CA_"; break;
                    case "Colorado": id += "CO_"; break;
                    case "Connecticut": id += "CT_"; break;
                    case "Delaware": id += "DE_"; break;
                    case "Florida": id += "FL_"; break;
                    case "Georgia": id += "GA_"; break;
                    case "Hawaii": id += "HI_"; break;
                    case "Idaho": id += "ID_"; break;
                    case "Illinois": id += "IL_"; break;
                    case "Indiana": id += "IN_"; break;
                    case "Iowa": id += "IA_"; break;
                    case "Kansas": id += "KS_"; break;
                    case "Kentucky": id += "KY_"; break;
                    case "Louisiana": id += "LA_"; break;
                    case "Maine": id += "ME_"; break;
                    case "Maryland": id += "MD_"; break;
                    case "Massachusetts": id += "MA_"; break;
                    case "Michigan": id += "MI_"; break;
                    case "Minnesota": id += "MN_"; break;
                    case "Mississippi": id += "MI_"; break;
                    case "Missouri": id += "MO_"; break;
                    case "Montana": id += "MT_"; break;
                    case "Nebraska": id += "NE_"; break;
                    case "Nevada": id += "NV_"; break;
                    case "New Hampshire": id += "NH_"; break;
                    case "New Jersey": id += "NJ_"; break;
                    case "New Mexico": id += "NM_"; break;
                    case "New York": id += "NY_"; break;
                    case "North Carolina": id += "NC_"; break;
                    case "North Dakota": id += "ND_"; break;
                    case "Ohio": id += "OH_"; break;
                    case "Oklahoma": id += "OK_"; break;
                    case "Oregon": id += "OR_"; break;
                    case "Pennsylvania": id += "PA_"; break;
                    case "Rhode Island": id += "RI_"; break;
                    case "South Carolina": id += "SC_"; break;
                    case "South Dakota": id += "SD_"; break;
                    case "Tennessee": id += "TN_"; break;
                    case "Texas": id += "TX_"; break;
                    case "Utah": id += "UT_"; break;
                    case "Vermont": id += "VT_"; break;
                    case "Virginia": id += "VA_"; break;
                    case "Washington": id += "WA_"; break;
                    case "West Virginia": id += "WV_"; break;
                    case "Wisconsin": id += "WI_"; break;
                    case "Wyoming": id += "WY_"; break;
                }

                Match match = Regex.Match(txtID.Text, @"^[a-zA-Z0-9]+$");
                if (!match.Success)
                {
                    MessageBox.Show("You have entered an invalid ID, please try again");
                    return;
                }
                id += txtID.Text;
            }
            else
            {
                Match match = Regex.Match(txtID.Text, @"^[0-9]+$");
                if (!match.Success)
                {
                    MessageBox.Show("You have entered an invalid ID, please try again");
                    return;
                }
                id = "GS_" + txtID.Text;
            }

            orderWindow.CustID = id;
            orderWindow.CommitChanges();
            orderWindow.buildPackageDataGrid();

            this.Close();
        }

        private void btnCancel_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }

        private void rbEagle_Checked(object sender, RoutedEventArgs e)
        {
            cmbState.IsEnabled = false;
        }

        private void rbDrivers_Checked(object sender, RoutedEventArgs e)
        {
            cmbState.IsEnabled = true;
        }
    }
}
